<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("crulFunction.php");
/**
 * parametri della pagina
 */
$pageArg = "Regole comuni";
$pageSubject = "regola comune";
$table = "com_rule";
$orderBy = "module, name";
$pref = "crul";
/**
 * Fine parametri della pagina
 */
$errMsg = "";
if(isset($_REQUEST["reqtype"])){
	if($_REQUEST["reqtype"] == "save"){
		$errMsg .= crulMod();
	}elseif(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
		$errMsg .= crulDel();
	}
	$module = sxREQUEST("module");
	$name = sxREQUEST("name");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$script = sxREQUEST("script");
	$admitted_sub = (isset($_REQUEST["admitted_sub"]))?"Y":"N";
}else{
	$module = sxREQ("mod");
	$name = sxREQ("name");
	$description = "";
	$note = "";
	$script = "";
	$admitted_sub = "N";
}

if($module != "" && $name != ""){
	$sSQL = "SELECT * FROM $table WHERE module='$module' AND name='$name'";
	if($objSDAO->query($sSQL, "sel")){
		if($row = $objSDAO->fetch("sel")){
			$module = $row["module"];
			$name =$row["name"];
			$description = $row["description"];
			$note = $row["note"];
			$script = $row["script"];
			$admitted_sub = $row["admitted_sub"];
			$upgrade_status = $row["upgrade_status"];
		}else{
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}
	}else{
		$url = $_SESSION[USEROBJ]->getHistory(-1, true);
		serverRedir($url);
	}
}else{
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
}
//output HTML
$aOpz = array("tit2"=>_t("Visualizza $pageSubject"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objMod =  document.getElementById("module");
			var objNam =  document.getElementById("name");
			var objDe =  document.getElementById("description");
			var objScr = document.getElementById("script");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objDe.value == ""){
				errMsg += "<?php echo _t("la description &egrave; obbligatoria.")?><br>";
				objDe.focus();
			}
			if(objMod.value == ""){
				errMsg += "<?php echo _t("il modulo &egrave; obbligatorio.")?><br>";
				objMod.focus();
			}
			if(objNam.value == ""){
				errMsg += "<?php echo _t("il nome &egrave; obbligatorio.")?><br>";
				objNam.focus();
			}
			if(objScr.value == ""){
				errMsg += "<?php echo _t("lo script &egrave; obbligatorio.")?><br>";
				objScr.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save new com rule");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"crulView.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\">");
echo("<input type=\"hidden\" name=\"name\" id=\"name\" value=\"$name\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");

//output comandi
$aCmd = array("alias"=>array("pag"=>"crulAliasList.php?mod=$module&name=$name",
										"title"=>_t("Lista degli alias"),
										"text"=>_t("alias"),
										"align"=>"L"
										),
					"scr"=>array("pag"=>"crulScript.php?mod=$module&name=$name",
										"title"=>_t("Elabora lo script"),
										"text"=>_t("script")
										),
					"mod"=>array("pag"=>"crulMod.php?mod=$module&name=$name",
										"title"=>_t("Modifica la")." $pageSubject",
										"text"=>_t("modifica")
										),
					"del"=>array("pag"=>"javascript:confirmDelete()",
										"title"=>_t("Elimina la")." $pageSubject",
										"text"=>_t("elimina")
										),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1)
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>"._t($pageSubject)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("module")." </td>" .
			"<td class=\"text01\">$module</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("nome")." </td>" .
			"<td class=\"text01\">$name</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")." </td>" .
			"<td class=\"text01\">".sxHE($description)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("sub ammesse")."</td>" .
			"<td class=\"text01\">".(($admitted_sub == "Y")?'yes':'no')."</td>" .
		"</tr>\n");
$aVal = array('S'=>'Strutturale',
	'M'=>'Modificabile non cancellabile',
	'C'=>'Modificabile e cancellabile',
	'D'=>'Cancellato',
	'U'=>'Modificato');
echo("<tr>" .
			"<td class=\"labl01\">"._t("upgrade status")."</td>" .
			"<td class=\"text01\">".((isset($aVal[$upgrade_status]))?$aVal[$upgrade_status]:$upgrade_status)."</td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border=\"0\">");
echo("<tr>" .
			"<td class=\"titl01\" width=\"50%\">"._t("script")." </td>" .
			"<td class=\"titl01\" width=\"50%\">"._t("note")."</td>" .			
		"</tr>\n");
echo("<tr>" .
			"<td class=\"text01\" valign='top'><pre class=\"text01\">".sxWW(sxHE($script))."</pre></td>" .
			"<td class=\"text01\" valign='top'><pre class=\"text01\">".sxWW(sxHE($note))."</pre></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
?>